import {buildPlainDefaultUrlConfig, createTableOptionUser, iTableOptionCacheData} from "plain-design";
import {http} from "./http";

export const useTableOption = createTableOptionUser({
  http,
  keyField: 'id',
  showRows: 10,
  pageSizeOptions: [10, 20, 50, 100, 200, 500],
  editType: 'inline',
  copyDefaultExcludeKeys: ['id', 'createdBy', 'createdAt', 'updatedBy', 'updatedAt'],
  getDefaultUrlConfig: buildPlainDefaultUrlConfig(),
  filterFormColumn: 3,
  showRowsMode: 'fixed',
  ...(() => {
    const STORAGE_KEY = '@@TABLE_PRO_CACHE';
    const str = window.localStorage.getItem(STORAGE_KEY);
    const totalCacheData = !str ? {} : JSON.parse(str);
    const getCache = (tableId: string): iTableOptionCacheData => {
      return totalCacheData[tableId + window.location.pathname + window.location.hash];
    };
    const setCache = (cacheData: iTableOptionCacheData) => {
      totalCacheData[cacheData.tableId + window.location.pathname + window.location.hash] = cacheData;
      window.localStorage.setItem(STORAGE_KEY, JSON.stringify(totalCacheData));
    };
    return { getCache, setCache };
  })(),
});
